﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Runtime.InteropServices;
namespace Tailoring
{
    public partial class mdistrip : Form
    {
        SqlConnection conn = new SqlConnection("Server= localhost; database=tailoringmng; integrated Security=true");
        DataTable dt;
        DataTable dt2;
        public string key { get { return txtnamesearch.Text; } }
        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HTCAPTION = 0x2;
        [DllImport("User32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("User32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        public mdistrip()
        {
            InitializeComponent();
        }
        public void dgfilter(string filterstr)
        {

            {
                try
                {

                    SqlConnection conn = new SqlConnection("Server= localhost; database=tailoringmng; integrated Security=true");
                    SqlCommand cmd = new SqlCommand("undonetask", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlDataAdapter da = new SqlDataAdapter(cmd);

                    if (conn == null && conn.State == ConnectionState.Closed)
                    {
                        conn.Open();
                    }

                    dt = new DataTable("myTable");
                    da.Fill(dt);

                    dgridstatus.DataSource = dt.DefaultView;

                    DataView dv = new DataView(dt);
                    BindingSource bs = new BindingSource();
                    bs.DataSource = dv;
                    dv.Table = dt;
                    bs.Filter = filterstr;
                    //dv.RowStateFilter = DataViewRowState.ModifiedCurrent;


                    dgridstatus.DataSource = bs;
                    dgridstatus.Tag = bs;
                    string rowcount=dgridstatus.RowCount.ToString();
                    lbundone.Text = rowcount;
                    label13.Text = "Record Found:";
                    dgridstatus.Refresh();
                    conn.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);

                }
            }
        }
        public void loaddata()
        {

            {
                try
                {


                    SqlCommand cmd = new SqlCommand("undonetask", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlDataAdapter da = new SqlDataAdapter(cmd);

                    if (conn == null && conn.State == ConnectionState.Closed)
                    {
                        conn.Open();
                    }

                    dt = new DataTable("myTable");
                    da.Fill(dt);
                    //while()
                    //{

                    //}
                    //BindingSource bs = new BindingSource();
                    //bs.DataSource = dt;
                    dgridstatus.DataSource = dt.DefaultView;
                    lbundone.Text = dgridstatus.RowCount.ToString();
                    label13.Text = "Total Records:";
                    conn.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);

                }
            }
        }
        public void loaddatadg2()
        {

            {
                try
                {

                    SqlConnection conn = new SqlConnection("Server= localhost; database=tailoringmng; integrated Security=true");
                    SqlCommand cmd = new SqlCommand("donetask", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlDataAdapter da = new SqlDataAdapter(cmd);

                    if (conn == null && conn.State == ConnectionState.Closed)
                    {
                        conn.Open();
                    }

                    dt2 = new DataTable("myTable");
                    da.Fill(dt2);
                    //BindingSource bs = new BindingSource();
                    //bs.DataSource = dt;
                    dgriddone.DataSource = dt2.DefaultView;
                    lbdone.Text = dgriddone.RowCount.ToString();
                    label15.Text = "Total Records:";
                    conn.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);

                }
            }
        }
        public void loaddatadg2filter()
        {

            {
                try
                {

                    SqlConnection conn = new SqlConnection("Server= localhost; database=tailoringmng; integrated Security=true");
                    SqlCommand cmd = new SqlCommand("donetask", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlDataAdapter da = new SqlDataAdapter(cmd);

                    if (conn == null && conn.State == ConnectionState.Closed)
                    {
                        conn.Open();
                    }

                    dt2 = new DataTable("myTable");
                    da.Fill(dt2);

                    dgriddone.DataSource = dt2.DefaultView;
                    
                    DataView dv = new DataView(dt2);
                    BindingSource bs = new BindingSource();
                    bs.DataSource = dv;
                    dv.Table = dt2;
                    bs.Filter = "Mobile="+ "'" + textBox2.Text + "'";
                    //dv.RowStateFilter = DataViewRowState.ModifiedCurrent;


                    dgriddone.DataSource = bs;
                    dgriddone.Tag = bs;
                    string rowcount = dgriddone.RowCount.ToString();
                    lbdone.Text = rowcount;
                    label15.Text = "Record Found:";
                    dgriddone.Refresh();
                    conn.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);

                }
            }
        }
        public void updateOrderTask(string cid, string oid, string statuskey, string ddatekey)
        {
            //try
            //{
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            //conn.Open();
            SqlCommand cmd = new SqlCommand("updatesatus", conn);
            cmd.Connection = conn;
            cmd.CommandType = CommandType.StoredProcedure;


            cmd.Parameters.AddWithValue("@orderStatus", statuskey);
            cmd.Parameters.AddWithValue("@id", cid);
            cmd.Parameters.AddWithValue("@ddate", ddatekey);
            cmd.Parameters.AddWithValue("@oid", oid);
            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.InsertCommand = cmd;
            int a = adapter.InsertCommand.ExecuteNonQuery();
            cmd.Connection.Close();
            if (a == -1)
            {
                MessageBox.Show("Update Successfull...");
            }

            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message);

            //}
            //finally
            {
                this.conn.Close();

            }

        }
        public string pidkey { get { return txtidsearch.Text; } }
        public string pnamekey { get { return txtnamesearch.Text; } }
        public string pmobilekey { get { return txtmobilesearch.Text; } }
        public string pcnickey { get { return txtcnicsearch.Text; } }
        public SqlDataReader loaddata(string a, string b)
        {
            string fild = a;
            string searchkey = b;
            {
                SqlConnection conn = new SqlConnection();
                conn = programcodes.getConnect();
                SqlCommand cmd = new SqlCommand();
                SqlDataReader reader;
                //string strquery=;
                cmd.CommandText = "SELECT * FROM tblclientinfo where'" + fild + "'='" + searchkey + "'";
                cmd.CommandType = CommandType.Text;
                cmd.Connection = conn;
                if (conn == null && conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }



                reader = cmd.ExecuteReader();
                // Data is accessible through the DataReader object here.
                while (reader.Read())
                {



                }
                conn.Close();
                return (reader);
            }
        }
        public string pidsearch
        {
            get { return txtidsearch.Text; }
        }
        public string pnamesearch
        {
            get { return txtnamesearch.Text; }
        }
        public string pmobilesearch
        {
            get { return txtmobilesearch.Text; }
        }
        public string pcnicsearch
        {
            get { return txtcnicsearch.Text; }
        }
        private void mdistrip_Load(object sender, EventArgs e)
        {
            loaddata();
            loaddatadg2();
        }

        private void label11_Click(object sender, EventArgs e)
        {
            searchdialog sd = new searchdialog();
            sd.ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            frmviewpemaish vp = new frmviewpemaish();
            vp.psearchkey = txtidsearch.Text;
            vp.MdiParent = this.ParentForm;

            vp.Show();
            string str = "SELECT     dbo.tblclientinfo.*, dbo.tblpemaish.* FROM dbo.tblpemaish INNER JOIN dbo.tblclientinfo ON dbo.tblpemaish.clientId = dbo.tblclientinfo.clientId WHERE(dbo.tblclientinfo.clientId) =@Key";
            vp.loadclient(txtidsearch.Text, str);
            //this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            frmviewpemaish vp = new frmviewpemaish();
            vp.psearchkey = txtnamesearch.Text;
            vp.MdiParent = this.ParentForm;
            vp.pnamekey = key;
            vp.Show();
            string str = "SELECT     dbo.tblclientinfo.*, dbo.tblpemaish.* FROM dbo.tblpemaish INNER JOIN dbo.tblclientinfo ON dbo.tblpemaish.clientId = dbo.tblclientinfo.clientId WHERE(dbo.tblclientinfo.name) =@Key";
            vp.loadclient(txtnamesearch.Text, str);

            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            frmviewpemaish vp = new frmviewpemaish();
            vp.psearchkey = txtmobilesearch.Text;
            vp.MdiParent = this.ParentForm;

            vp.Show();
            string str = "SELECT     dbo.tblclientinfo.*, dbo.tblpemaish.* FROM dbo.tblpemaish INNER JOIN dbo.tblclientinfo ON dbo.tblpemaish.clientId = dbo.tblclientinfo.clientId WHERE(dbo.tblclientinfo.mobile) =@Key";
            vp.loadclient(txtmobilesearch.Text, str);
            this.Close();
            //string searchcri = "mobile";
            //string searchkey = txtmobilesearch.Text;
            //loaddata(searchkey, searchcri);
            ////this.Close();
            //frmcus cs = new frmcus();

            //cs.pmobilekey = pmobilekey;
            //cs.Show();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            frmviewpemaish vp = new frmviewpemaish();
            vp.psearchkey = txtcnicsearch.Text;
            vp.MdiParent = this.ParentForm;

            vp.Show();
            string str = "SELECT     dbo.tblclientinfo.*, dbo.tblpemaish.* FROM dbo.tblpemaish INNER JOIN dbo.tblclientinfo ON dbo.tblpemaish.clientId = dbo.tblclientinfo.clientId WHERE(dbo.tblclientinfo.cnic) =@Key";
            vp.loadclient(txtcnicsearch.Text, str);
            this.Close();
        }

        private void btnpemaish_Click(object sender, EventArgs e)
        {
            frmentry entry = new frmentry();
            entry.MdiParent = this.ParentForm;

            entry.Show();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            searchdialog sd = new searchdialog();

            sd.ShowDialog();


        }

        private void button6_Click(object sender, EventArgs e)
        {
            frmorder order = new frmorder();
            order.MdiParent = this.ParentForm;
            order.Show();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            Searchpopup spop = new Searchpopup();
            //spop.MdiParent = this.ParentForm;
            spop.Show();
            //spop.TopMost = true;
        }

        private void dgridstatus_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int row = e.RowIndex;

            if (e.ColumnIndex == dgridstatus.Columns["btnDone"].Index && e.RowIndex >= 0)
            {
                //string searchcri = "clientId";
                //string searchkey = dt.Rows[row]["ID"].ToString();

                frmcus cs = new frmcus();
                cs.pidkey = dt.Rows[row]["ID"].ToString();
                cs.ddatekey = dt.Rows[row]["Delivery"].ToString();
                string daylelf = dt.Rows[row]["Days_left"].ToString();

                cs.Show();
                cs.MdiParent = this.ParentForm;
                cs.dataloadbyddate();
                cs.accountinfo();
                

               
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            frmdontask donetask = new frmdontask();
            donetask.Show();
        }

        private void dgriddone_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int donerow = e.RowIndex;

            if (e.ColumnIndex == dgriddone.Columns["btnview"].Index && e.RowIndex >= 0)
            {


                frmdontask donet = new frmdontask();
                string idkey = dt2.Rows[donerow]["ID"].ToString();
                //string ddatekey = dt2.Rows[donerow]["DeliveryDate"].ToString();
                donet.pid = dt2.Rows[donerow]["ID"].ToString();
                donet.porderid = dt2.Rows[donerow]["orderId"].ToString();
                
                
                //donet.ddatekey = dt.Rows[row]["Delivery"].ToString();
                //string daylelf = dt.Rows[row]["Days_left"].ToString();
                donet.Show();
                donet.orderDetail(idkey);
                loaddata();
                loaddatadg2();

            }
            if (e.ColumnIndex == dgriddone.Columns["btnundone"].Index && e.RowIndex >= 0)
            {
                string idkey = dt2.Rows[donerow]["ID"].ToString();
                string ddate = dt2.Rows[donerow]["DeliveryDate"].ToString();
                string orderid = dt2.Rows[donerow]["orderID"].ToString();
                string statusorder = "Undone";
                updateOrderTask(idkey, orderid, statusorder, ddate);
                loaddata();
                loaddatadg2();
            }
        }

        private void txtidsearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Convert.ToInt32(e.KeyChar) == 13)
            {
                if (txtidsearch.Text.Length > 0)
                {
                    frmviewpemaish vp = new frmviewpemaish();
                    //vp.psearchkey = psearch;
                    vp.MdiParent = this.ParentForm;

                    vp.Show();
                    string str = "SELECT     dbo.tblclientinfo.*, dbo.tblpemaish.* FROM dbo.tblpemaish INNER JOIN dbo.tblclientinfo ON dbo.tblpemaish.clientId = dbo.tblclientinfo.clientId WHERE(dbo.tblclientinfo.clientId) =@Key";

                    vp.loadclient(txtidsearch.Text, str);
                    
                }
                else
                {
                    this.Close();
                }
            }
        }

        private void txtnamesearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Convert.ToInt32(e.KeyChar) == 13)
            {
                if (txtnamesearch.Text.Length > 0)
                {
                    frmviewpemaish vp = new frmviewpemaish();
                    //vp.psearchkey = txtnamesearch.Text;
                    vp.pnamekey = key;
                    vp.MdiParent = this.ParentForm;

                    vp.Show();
                    string str = "SELECT     dbo.tblclientinfo.*, dbo.tblpemaish.* FROM dbo.tblpemaish INNER JOIN dbo.tblclientinfo ON dbo.tblpemaish.clientId = dbo.tblclientinfo.clientId WHERE(dbo.tblclientinfo.name) =@Key";

                    vp.loadclient(txtnamesearch.Text, str);
                  
                }
                else
                {
                    this.Close();
                }
            }
        }

        private void txtmobilesearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Convert.ToInt32(e.KeyChar) == 13)
            {
                if (txtmobilesearch.Text.Length > 0)
                {
                    frmviewpemaish vp = new frmviewpemaish();
                    //vp.psearchkey = psearch;
                    vp.MdiParent = this.ParentForm;

                    vp.Show();
                    string str = "SELECT     dbo.tblclientinfo.*, dbo.tblpemaish.* FROM dbo.tblpemaish INNER JOIN dbo.tblclientinfo ON dbo.tblpemaish.clientId = dbo.tblclientinfo.clientId WHERE(dbo.tblclientinfo.mobile) =@Key";

                    vp.loadclient(txtmobilesearch.Text, str);
                    ;
                }
                else
                {
                    this.Close();
                }
                }
             }

        private void txtcnicsearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Convert.ToInt32(e.KeyChar) == 13)
            {
                if (txtcnicsearch.Text.Length > 0)
                {
                    frmviewpemaish vp = new frmviewpemaish();
                    vp.psearchkey = txtcnicsearch.Text;
                    vp.MdiParent = this.ParentForm;

                    vp.Show();
                    string str = "SELECT     dbo.tblclientinfo.*, dbo.tblpemaish.* FROM dbo.tblpemaish INNER JOIN dbo.tblclientinfo ON dbo.tblpemaish.clientId = dbo.tblclientinfo.clientId WHERE(dbo.tblclientinfo.cnic) =@Key";

                    vp.loadclient(txtcnicsearch.Text, str);
                    
                }
                else
                {
                    this.Close();
                }
            }
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            string a = "ID=" + "'" + textBox1.Text + "'";
            if (Convert.ToInt32(e.KeyChar) == 13)
            {
                if(textBox1.Text.Length>0)
                    
                    dgfilter(a);
                else
                    loaddata();
                
            }
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Convert.ToInt32(e.KeyChar) == 13)
            {
                if (textBox2.Text.Length > 0)
                { loaddatadg2filter(); }
                else
                {loaddatadg2();}
                
            }
        }

        private void mdistrip_Activated(object sender, EventArgs e)
        {
            loaddata();
            loaddatadg2();
        }
        }
}


